﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EntitiesModel;
using HPModel;

namespace ITMan
{
    public partial class FormHPSync : Form
    {
        public FormHPSync()
        {
            InitializeComponent();
        }

        private void hpCheck(VW_MacToDP imcUser)
        {
            var hpContext = new HPEntities();
            IQueryable<T_sys_cntuser> hpUsers = from u in hpContext.T_sys_cntusers
                                                where u.User_mac.ToUpper() == imcUser.MAC_ADDRESS
                                                select u;
            if (hpUsers.Any())
            {
                T_sys_cntuser hpUser = hpUsers.FirstOrDefault();
                if (hpUser.LoginID != imcUser.USER_NAME)
                {
                    //更新
                    WriteLog(string.Format("U\tMAC地址{0}由用户{1}:{2}变更为{3}:{4}", hpUser.User_mac, hpUser.LoginID,
                                           hpUser.User_name, imcUser.USER_NAME, imcUser.FULL_NAME));
                    hpUser.LoginID = imcUser.USER_NAME;
                    hpUser.User_name = imcUser.FULL_NAME;
                    hpContext.SaveChanges();

                    
                }
            }

            //因互普系统中用户处理机制问题，是以IP为准。所以写入的用户不能用。遂只更新其系统已有用户
//            else
//            {
//                UInt32 maxIp = hpContext.T_sys_cntusers.Max(e => e.User_ip);
//                maxIp += 1;
//                //插入
//                T_sys_cntuser hpUser = new T_sys_cntuser();
//                hpUser.LoginID = imcUser.USER_NAME;
//                hpUser.User_name = imcUser.FULL_NAME;
//                hpUser.User_mac = imcUser.MAC_ADDRESS.ToLower();
//                hpUser.User_ip = maxIp;
//                hpUser.Gid = 1;
//                hpContext.Add(hpUser);
//                hpContext.SaveChanges();
//            
//                WriteLog(string.Format("I\tMAC地址{0}绑定至用户{1}:{2}", hpUser.User_mac, hpUser.LoginID, hpUser.User_name));
//            }

        }

        private void hpOnlineUserCheck(VW_MacToDP imcUser)
        {
            var hpContext = new HPEntities();
            IQueryable<T_sys_useronline> hpOnlineUsers = from u in hpContext.T_sys_useronlines
                                                         where u.Mac.ToUpper() == imcUser.MAC_ADDRESS
                                                         select u;
            if (hpOnlineUsers.Any())
            {
                T_sys_useronline hpOnlineUser = hpOnlineUsers.FirstOrDefault();
                if (hpOnlineUser.Name != imcUser.USER_NAME)
                {
                    //更新
                    WriteLog(string.Format("U\tMAC地址{0}由用户{1}:{2}变更为{3}:{4}", hpOnlineUser.Mac, hpOnlineUser.LoginID,
                                           hpOnlineUser.Name, imcUser.USER_NAME, imcUser.FULL_NAME));
                    hpOnlineUser.LoginID = imcUser.USER_NAME;
                    hpOnlineUser.Name = imcUser.FULL_NAME;

                    hpContext.SaveChanges();

                    
                }
            }
        }

        private void WriteLog(string message)
        {
            string logPath = AppDomain.CurrentDomain.BaseDirectory;
            if (!Directory.Exists(logPath + @"\log"))
            {
                Directory.CreateDirectory(logPath + @"\log");
            }
            tbLog.AppendText(string.Format("{0}\t" + message + System.Environment.NewLine, System.DateTime.Now));
            tbLog.Refresh();

            //更新tbLog中显示内容
            StreamWriter dout =
                new StreamWriter(string.Format(logPath + @"\log\{0}.log", DateTime.Today.ToString("yyyy-MM-dd")), true);
            dout.Write("{0}\t" + message + System.Environment.NewLine, System.DateTime.Now);
            dout.Close();
        }

        private void btSync_Click(object sender, EventArgs e)
        {
            var imcConText = new EadEntitiesModel();

            //读取用户、MAC资料
            IQueryable<VW_MacToDP> imcUser = imcConText.VW_MacToDPs;
            WriteLog("SYS\t加载IMC用户资料！");

            foreach (var imc in imcUser)
            {
                //WriteLog(string.Format("SYS\t开始处理用户{0}", imc.USER_NAME));
                hpCheck(imc);
            }
            WriteLog("SYS\t同步完成！################");
            MessageBox.Show(string.Format("同步完成！"));
        }

        private void btSyncOnlineUser_Click(object sender, EventArgs e)
        {
            var imcConText = new EadEntitiesModel();

            //读取用户、MAC资料
            IQueryable<VW_MacToDP> imcUser = imcConText.VW_MacToDPs;
            WriteLog("SYS\t加载IMC用户资料！");

            foreach (var imc in imcUser)
            {
                //WriteLog(string.Format("SYS\t开始处理用户{0}", imc.USER_NAME));
                hpOnlineUserCheck(imc);
            }
            WriteLog("SYS\t同步完成！################");
            MessageBox.Show(string.Format("同步完成！"));
        }
    }
}